package com.lagou.edu.interceptors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author xsl
 * @date 2020/7/22 22:12
 * @Description: 登录拦截器
 */
@Configuration
public class LoginHandlerInterceptor implements HandlerInterceptor {
    private static final Logger logger = LoggerFactory.getLogger(LoginHandlerInterceptor.class);

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        logger.info("--------------------HandlerInterceptor1.preHandler-------------------------------------- ");
        String requestUri = request.getRequestURI();
        if (requestUri.indexOf("editResumeInfo.action") > 0 || requestUri.indexOf("addResumeInfo.action") > 0
                || requestUri.indexOf("deleteResumeInfo.action") > 0) {
            HttpSession session = request.getSession();
            String username = (String) session.getAttribute("username");
            if (!StringUtils.isEmpty(username)) {
                //登录成功
                return true;
            } else {
                //调转到登录页
                request.getRequestDispatcher("/index.jsp").forward(request, response);
                return false;
            }
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        logger.info("--------------------HandlerInterceptor1.preHandler-------------------------------------- ");

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        logger.info("--------------------HandlerInterceptor1.preHandler-------------------------------------- ");
    }
}
